﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Xml;
using JumbotCms.Common;

namespace JumbotCms.WebFile.User
{
    public partial class _forum_index : JumbotCms.API.UserCenter
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User_Load("", "html");
            if (q("act") != "save")
            {
                if (q("oper") == "form")
                {
                    Response.Write(GetPageHtml("user_forum"));
                    Response.End();
                }
                if (site.ForumPath != "" && Jumbot.Common.Cookie.GetValue("dnt") == null)
                {
                    bool _AutoLogining = false;
                    string _ForumAutoRegister = Jumbot.Common.XmlCOM.ReadConfig("~/_data/config/api", "ForumAutoRegister");
                    if (_ForumAutoRegister == "user")//表示自动注册论坛用户
                        _AutoLogining = true;
                    else
                    {
                        JumbotCms.DataObject.User _User = new JumbotCms.DataObject.User();
                        if (_User.Writer(UserId, doh))
                            _AutoLogining = true;
                    }
                    if (_AutoLogining == true)
                    {
                    //登陆社区
                    doh.Reset();
                    doh.ConditionExpress = "id=@id and state=1";
                    doh.AddConditionParameter("@id", UserId);
                    object[] _forumInfo = doh.GetValues("jcms_normal_user", "ForumName,ForumPass");
                    if (_forumInfo[0].ToString() == "")//还没绑定论坛
                    {
                        if (Jumbot.API.Discuz.Command.Login(UserName, UserPass, 1))
                        {
                            doh.Reset();
                            doh.ConditionExpress = "id=@id and state=1";
                            doh.AddConditionParameter("@id", UserId);
                            doh.AddFieldItem("ForumName", UserName);
                            doh.AddFieldItem("ForumPass", UserPass);
                            doh.Update("jcms_normal_user");
                        }
                        else
                        {
                            bool IsSuc = Jumbot.API.Discuz.Command.Register(UserName, 0, "", UserPass, "");
                            if (IsSuc)
                            {
                                //注册成功
                                doh.Reset();
                                doh.ConditionExpress = "id=@id and state=1";
                                doh.AddConditionParameter("@id", UserId);
                                doh.AddFieldItem("ForumName", UserName);
                                doh.AddFieldItem("ForumPass", UserPass);
                                doh.Update("jcms_normal_user");
                                Jumbot.API.Discuz.Command.Login(UserName, UserPass, 1);
                            }
                        }
                    }
                    else
                    {
                        if (!Jumbot.API.Discuz.Command.Login(_forumInfo[0].ToString(), _forumInfo[1].ToString(), 1))
                        {
                            Response.Write(GetPageHtml("user_forum"));
                            Response.End();
                        }
                    }
}
                }
                Response.Redirect(site.ForumUrl);
            }
            else
            {
                if (!CheckFormUrl())
                    Response.End();
                doh.Reset();
                doh.ConditionExpress = "id=@id and state=1";
                doh.AddConditionParameter("@id", UserId);
                doh.AddFieldItem("ForumName", f("txtForumName"));
                doh.AddFieldItem("ForumPass", GetHash32(f("txtForumPass1")));
                doh.Update("jcms_normal_user");
                Response.Write("JumbotCms.Alert('修改成功', '1', \"window.location='index.aspx';\");");
            }

        }
    }
}
